package com.cgsd.heyu.widgets.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.cgsd.heyu.R;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.ui.webview.CommonH5Activity;

/**
 * @author xiaoxing
 * Description 服务协议弹窗
 * CreateData: 2020/11/2
 */
public class AgreementDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private ConfirmListener confirmListener;


    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }


    public AgreementDialog(Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 适配某些机器出现不居中问题。
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }

        setCanceledOnTouchOutside(false);
        setOnKeyListener(new DialogInterface.OnKeyListener(){
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode== KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0) {
                    ((Activity)mContext).finish();
                    return true;
                }
                else{
                    return false;
                }
            }
        });
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_agreement, null);
        setContentView(view);
        TextView tvServer = view.findViewById(R.id.tv_server);
        TextView tvPrivacy = view.findViewById(R.id.tv_privacy);
        TextView tvAgree = view.findViewById(R.id.tv_agree);
        TextView tvDisagree = view.findViewById(R.id.tv_disagree);
        //下划线
        tvServer.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        //抗锯齿
        tvServer.getPaint().setAntiAlias(true);

        //下划线
        tvPrivacy.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        //抗锯齿
        tvPrivacy.getPaint().setAntiAlias(true);

        tvAgree.setOnClickListener(this);
        tvDisagree.setOnClickListener(this);
        tvServer.setOnClickListener(this);
        tvPrivacy.setOnClickListener(this);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.6
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_agree:
                if (confirmListener != null){
                    confirmListener.doConfirm();
                }
                break;
            case R.id.tv_disagree:
                if(confirmListener != null){
                    confirmListener.doCancel();
                }
                break;
            case R.id.tv_server:
                CommonH5Activity.actionStart((Activity) mContext,AppConfig.USER_AGREEMENT,mContext.getString(R.string.user_agreement));
                break;
            case R.id.tv_privacy:
                CommonH5Activity.actionStart((Activity) mContext, AppConfig.PRIVACY_PROTOCOL,mContext.getString(R.string.privacy_agreement));
                break;
            default:
        }
    }


    @Override
    public void show() {
        super.show();
    }

    public interface ConfirmListener {
        /**
         * 同意
         */
        void doConfirm();

        /**
         * 不同意
         */
        void doCancel();
    }

}
